var addHandler = (function(){  //事件绑定，且只做一次判断在，加载时
    if(document.addEventListener){
        return function(elem, type, handler){
            return elem.addEventListener(type, handler, false);
        }   
    }else if(document.attachEvent){  //ie6 7 8
        return function(elem, type, handler){
            return elem.attachEvent('on'+type,function(){
                handler.apply(elem, arguments);    //修复this的指向
            });
        }
    }else{
        return function(elem, type, handler){
            elem["on"+type]=handler;
        }
    }
})();

var $ = function(id){
	return document.getElementById(id);
};

var cleanWhitespace = function (oEelement){
 for(var i=0;i<oEelement.childNodes.length;i++){
    var node=oEelement.childNodes[i];
    if(node.nodeType == 3 && !/\S/.test(node.nodeValue)){node.parentNode.removeChild(node)}
  }
};

var createTip = function(ele, content, style){
	cleanWhitespace(ele.parentNode);
	if(ele.nextSibling && ele.nextSibling.className == "tip"){
		var span = ele.nextSibling;
	}else{
		var span = document.createElement("span");
		span.className = "tip";
	}
	style["color"] = style["color"] || "red";
	style["display"] = "inline";
	for(var value in style){
		span.style[value] = style[value];
	}
	span.innerHTML = content;
	ele.parentNode.appendChild(span);
};

var hideTip = function(ele){
	cleanWhitespace(ele.parentNode);
	if(ele.nextSibling && ele.nextSibling.className == "tip"){
		ele.nextSibling.style.display = "none";
	}
};

var formate = {
	email: /^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/
}

var checkinput = function(showRequired, fontStyle){
	this.showRequired = showRequired;
	this.fontStyle = fontStyle;
};


checkinput.prototype.add = function(o){
	var ele = $(o["id"]);
	var isSize = o["min"] || o["max"];
	var that = this;

	if(o["required"] && this.showRequired){
		createTip(ele, "*", this.fontStyle);
	}

	
	if(o["defalut"]){
		ele.value = o["defalut"];
		ele.style.color = "#ccc";
	}

	addHandler(ele, "click", function(){
		if(this.value == o["defalut"]){
			this.value = "";
			this.style.color = "#000";			
		}
	});
	
	addHandler(ele, "blur", function(){ 
		var valueEmpty = this.value == "";
		
		if(o["required"] && valueEmpty){  //为空时的验证
		 	createTip(this, "未填写", that.fontStyle);
		}

		if(valueEmpty){
			this.value = o["defalut"];
			this.style.color = "#ccc";
		}else{
			if(isSize){
				var size = this.value.length;
				var min = o["min"] && o["min"] > size && "至少" + o["min"] + "位";//小于最小位数时
				var max = o["max"] && o["max"] < size && "至多" + o["max"] + "位";//大于最大位数时
				min && createTip(this, min, that.fontStyle);
				max && createTip(this, max, that.fontStyle);
			}
			var type = formate[o["type"]];
			( ( type 
				&& !type.test(this.value)) 
			  || ( !type 
			  	   && toString.call(o["type"]).indexOf("RegExp") > -1 
			  	   && !o["type"].test(this.value) ) 
			) 
			&& createTip(this, "格式错误", that.fontStyle);
		}

	});

	addHandler(ele, "keydown", function(){
		hideTip(this);
	});
	return this;
};


var test = new checkinput(
	true, 
	{   //文字显示的样式
		color: "pink",
		fontSize: "12px",
		fontFamily: "微软雅黑",
		paddingLeft: "4px"
	}
);


test.add({
	id: "name",
	required: true,
	min: 8,
	max: 15,
	type: "email",
	defalut: "点击消失"
});